// components/processLine/index.ts
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    courseProcessList:{
      type:Array,
      value:[],
      observer(newVal:any){
        this.setData({
          courseProcessListCopy:newVal
        })
      }
    },
    isTk:{
      type:Boolean,
      value:false
    },
  },
 
  /**
   * 组件的初始数据
   */
  data: {
    courseProcessListCopy:[],
    userType:getApp().getUserType()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    checkedCourse(e:any){
      const type = this.data.userType
      switch(type){
        case '01':
          const {lessonid,coursename} = e.currentTarget.dataset
          wx.navigateTo({
            url:`/teacher_course_package/pages/teacherCourseDetail/teacherCourseDetail?lessonId=${lessonid}&title=${coursename}`
          })
          break;
        case '02': 
          if(this.properties.isTk){
            const {datetime,fristindex,sencondindex} = e.currentTarget.dataset
            console.log(datetime,fristindex,sencondindex,'====',this.data.courseProcessListCopy); 
            const courseProcessListCopy = this.data.courseProcessListCopy as any
            courseProcessListCopy.forEach((val:any)=>{
              if(val.dateTime === datetime){
                val.newList.forEach((item:any,index:number)=>{
                  if(index === fristindex){
                    item.plan.forEach((par:any,num:number)=>{
                      if(num === sencondindex){
                        par.checked = !par.checked
                      }
                    })
                  }
                })
              }
            })
             this.setData({
              courseProcessListCopy
             })
          }
          break;
      }
    },
    confirmCBtn(){
      const courseProcessListCopy = this.data.courseProcessListCopy as any
      const result = courseProcessListCopy.reduce((acc:any, item:any) => {
        const ids = item.newList.reduce((innerAcc:any, newItem:any) => {
          const filteredIds = newItem.plan
            .filter((planItem:any) => planItem.checked)
            .map((planItem:any) => planItem.lessonId);
          return innerAcc.concat(filteredIds);
        }, []);
        return acc.concat(ids);
      }, []);
      if(result.length > 0){
        const pages  = getCurrentPages()
       const currPage = pages[pages.length - 1];   //当前页面
       const prevPage = pages[pages.length - 2];  //上一个页面
       prevPage.setData({
        leaveCourseList:result
      });
           wx.navigateBack({
              delta: 1,
            })
      }else{
        wx.Utils.toast("请选择课程！")
      }
    }
  }
})

